Trò chơi đua xe động vật trong UNITY Engine
114.851 lượt xem;
1 using UnityEngine;
2 using System.Collections;
3
4 public class Bezier : MonoBehaviour
5 {
6 private Vector2[] vects;
7 private float stateTime;
8 private Vector2 position;
9 private float duration;
10 private float dDuration;
11
12 private bool isRunning;
13
14 public void Start()
15 {
16 isRunning = true;
17 }
18
19 public void Update()
20 {
21 if (isRunning)
22 {
23 position = bezier(Time.deltaTime);
24 transform.localPosition = new Vector3(position.x, position.y, transform.localPosition.z);
25 }
26 }
27
28 //duration default = 1
29 public void setBezier(params Vector2[] vects)
30 {
31 this.vects = vects;
32 this.duration = 1;
33 this.position = vects[0];
34 }
35
36 public void setBezier(float duration, params Vector2[] vects)
37 {
38 this.vects = vects;
39 this.duration = duration == -1 ? 1 : duration;
40 this.dDuration = duration;
41 this.position = vects[0];
42 }
43
44 public Vector2 bezier(float delta)
45 {
46 stateTime += delta;
47 if (stateTime >= duration && dDuration != -1)
48 {
49 return vects[vects.Length - 1];
50 }
51 switch (vects.Length)
52 {
53 case 3:
54 position = bezier2(stateTime / duration);
55 break;
56 case 4:
57 position = bezier3(stateTime / duration);
58 break;
59 default:
60 break;
61 }
62 return position;
63 }
64
65 private Vector2 bezier2(float t)
66 {
67 float x = (1 - t) * (1 - t) * vects[0].x + 2 * t * (1 - t) * vects[1].x + t * t * vects[2].x;
68 float y = (1 - t) * (1 - t) * vects[0].y + 2 * t * (1 - t) * vects[1].y + t * t * vects[2].y;
69 return new Vector2(x, y);
70 }
71
72 private Vector2 bezier3(float t)
73 {
74 float x = (1 - t) * (1 - t) * (1 - t) * vects[0].x + 3 * t * (1 - t) * (1 - t) * vects[1].x
75 + 3 * t * t * (1 - t) * vects[2].x + t * t * t * vects[3].x;
76 float y = (1 - t) * (1 - t) * (1 - t) * vects[0].y + 3 * t * (1 - t) * (1 - t) * vects[1].y
77 + 3 * t * t * (1 - t) * vects[2].y + t * t * t * vects[3].y;
78 return new Vector2(x, y);
79 }
80
81 public void setRunning(bool isRunning)
82 {
83 this.isRunning = isRunning;
84 }
85 }
2 using System.Collections;
3
4 public class Bezier : MonoBehaviour
5 {
6 private Vector2[] vects;
7 private float stateTime;
8 private Vector2 position;
9 private float duration;
10 private float dDuration;
11
12 private bool isRunning;
13
14 public void Start()
15 {
16 isRunning = true;
17 }
18
19 public void Update()
20 {
21 if (isRunning)
22 {
23 position = bezier(Time.deltaTime);
24 transform.localPosition = new Vector3(position.x, position.y, transform.localPosition.z);
25 }
26 }
27
28 //duration default = 1
29 public void setBezier(params Vector2[] vects)
30 {
31 this.vects = vects;
32 this.duration = 1;
33 this.position = vects[0];
34 }
35
36 public void setBezier(float duration, params Vector2[] vects)
37 {
38 this.vects = vects;
39 this.duration = duration == -1 ? 1 : duration;
40 this.dDuration = duration;
41 this.position = vects[0];
42 }
43
44 public Vector2 bezier(float delta)
45 {
46 stateTime += delta;
47 if (stateTime >= duration && dDuration != -1)
48 {
49 return vects[vects.Length - 1];
50 }
51 switch (vects.Length)
52 {
53 case 3:
54 position = bezier2(stateTime / duration);
55 break;
56 case 4:
57 position = bezier3(stateTime / duration);
58 break;
59 default:
60 break;
61 }
62 return position;
63 }
64
65 private Vector2 bezier2(float t)
66 {
67 float x = (1 - t) * (1 - t) * vects[0].x + 2 * t * (1 - t) * vects[1].x + t * t * vects[2].x;
68 float y = (1 - t) * (1 - t) * vects[0].y + 2 * t * (1 - t) * vects[1].y + t * t * vects[2].y;
69 return new Vector2(x, y);
70 }
71
72 private Vector2 bezier3(float t)
73 {
74 float x = (1 - t) * (1 - t) * (1 - t) * vects[0].x + 3 * t * (1 - t) * (1 - t) * vects[1].x
75 + 3 * t * t * (1 - t) * vects[2].x + t * t * t * vects[3].x;
76 float y = (1 - t) * (1 - t) * (1 - t) * vects[0].y + 3 * t * (1 - t) * (1 - t) * vects[1].y
77 + 3 * t * t * (1 - t) * vects[2].y + t * t * t * vects[3].y;
78 return new Vector2(x, y);
79 }
80
81 public void setRunning(bool isRunning)
82 {
83 this.isRunning = isRunning;
84 }
85 }
duration default = 1